using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTLAutomateGUI.Controls.FormControls;
using DTLAutomateGUI.Controls.LayoutControls;
using DTLAutomateGUI.Controls;


namespace DTLAutomateGUI
{
    public partial class Design : Form
    {
		private MainScreen mainScreen;
		private BlockPropertiesGroupBox blockPropertiesGroupBox;
		private BorderPropertiesGroupBox borderPropertiesGroupBox;

		public event FileActionPerformedDelegate fileActionPerformed;

        public Design(MainScreen mainScreen)
        {
			this.mainScreen = mainScreen;
            InitializeComponent();
			InitializeGroupBoxes();
			this.layoutCanvas.TheLayout = this.mainScreen.dataSetLayout;
			this.layoutCanvas.selectionChanged += new SelectedEvent(layoutCanvas_selectionChanged);
        }

		private void InitializeGroupBoxes()
		{
			this.blockPropertiesGroupBox = new DTLAutomateGUI.Controls.FormControls.BlockPropertiesGroupBox();
			this.blockPropertiesGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
				| System.Windows.Forms.AnchorStyles.Right)));
			this.blockPropertiesGroupBox.Location = new System.Drawing.Point(3, 3);
			this.blockPropertiesGroupBox.Name = "blockPropertiesGroupBox";
			this.blockPropertiesGroupBox.Size = new System.Drawing.Size(209, 536);
			this.blockPropertiesGroupBox.TabIndex = 0;

			this.borderPropertiesGroupBox = new DTLAutomateGUI.Controls.FormControls.BorderPropertiesGroupBox();
			this.borderPropertiesGroupBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
				| System.Windows.Forms.AnchorStyles.Right)));
			this.borderPropertiesGroupBox.Location = new System.Drawing.Point(3, 3);
			this.borderPropertiesGroupBox.Name = "borderPropertiesGroupBox";
			this.borderPropertiesGroupBox.Size = new System.Drawing.Size(209, 536);
			this.borderPropertiesGroupBox.TabIndex = 0;
		}

		void layoutCanvas_selectionChanged(SelectEventArgs se)
		{
			this.mainSplitContainer.Panel2.Controls.Remove(this.blockPropertiesGroupBox);
			this.mainSplitContainer.Panel2.Controls.Remove(this.borderPropertiesGroupBox);

			if (se.source.Selected)
			{
				if (se.source is SimpleBlockControl)
				{
					this.blockPropertiesGroupBox.DataRow = ((SimpleBlockControl)se.source).DataRow;
					this.mainSplitContainer.Panel2.Controls.Add(this.blockPropertiesGroupBox);
					//this.mainSplitContainer.Invalidate();
					this.blockPropertiesGroupBox.Size = new System.Drawing.Size(this.mainSplitContainer.Panel2.Width-10, 536);
					this.blockPropertiesGroupBox.Focus();
				}
				else if (se.source is DumbBorderControl)
				{
					this.borderPropertiesGroupBox.DataRow = ((DumbBorderControl)se.source).DataRow;
					this.mainSplitContainer.Panel2.Controls.Add(this.borderPropertiesGroupBox);
					//this.mainSplitContainer.Invalidate();
					this.borderPropertiesGroupBox.Size = new System.Drawing.Size(this.mainSplitContainer.Panel2.Width - 10, 536);
					this.borderPropertiesGroupBox.Focus();
				}
			}
		}

		private void toolStripButtonSimpleBlock_Click(object sender, EventArgs e)
		{
			this.layoutCanvas.Mode = CanvasMode.Place;
			this.layoutCanvas.ObjectToPlace = CanvasObject.SimpleBlock;
		}

		private void toolStripButtonSimpleBorder_Click(object sender, EventArgs e)
		{
			this.layoutCanvas.Mode = CanvasMode.Place;
			this.layoutCanvas.ObjectToPlace = CanvasObject.SimpleBorder;
		}

		private void Design_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.Control && e.KeyCode == Keys.S)
			{
				if (this.fileActionPerformed != null)
				{
					this.fileActionPerformed(new FileActionEventArgs(FileAction.Save));
					e.Handled = true;
				}
			}
		}

		private void selectToolStripButton_Click(object sender, EventArgs e)
		{
			this.layoutCanvas.Mode = CanvasMode.Select;
		}

		private void layoutCanvas_modeChanged(CanvasMode mode)
		{
			switch (mode)
			{
				case CanvasMode.Select:
					this.resetToolBar();
					this.selectToolStripButton.Enabled = false;
					break;
				case CanvasMode.Delete:
					this.resetToolBar();
					this.deleteToolStripButton.Enabled = false;
					break;
				case CanvasMode.Connect:

					break;
				default:
					break;
			}
		}

		private void resetToolBar()
		{
			this.selectToolStripButton.Enabled = true;
			this.deleteToolStripButton.Enabled = true;
			this.toolStripButtonSensorBorder.Enabled = true;
			this.toolStripButtonSimpleBlock.Enabled = true;
			this.toolStripButtonSimpleBorder.Enabled = true;
			this.toolStripButtonTurnout2to1.Enabled = true;
			this.toolStripButtonTurnout3to1.Enabled = true;
			this.toolStripButtonTurnoutSingleCrossover.Enabled = true;
		}

		private void deleteToolStripButton_Click(object sender, EventArgs e)
		{
			this.layoutCanvas.Mode = CanvasMode.Delete;
		}
    }
}